package com.sofwin.controller;

import com.github.pagehelper.PageInfo;
import com.sofwin.Constants;
import com.sofwin.dto.UserDto;
import com.sofwin.pojo.SysDept;
import com.sofwin.pojo.SysRole;
import com.sofwin.pojo.SysUser;
import com.sofwin.service.DeptService;
import com.sofwin.service.RoleService;
import com.sofwin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("system/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private DeptService deptService;

    @RequestMapping("data")
    public String data(SysUser user, @RequestParam(required = true,defaultValue = "1") Integer pageNumber, @RequestParam(required = true,defaultValue = Constants.DEFAULT_PAGE_SIZE) Integer pageSize,
                        Model model){
       PageInfo<UserDto> page =  userService.selectUserByPage(user,pageNumber,pageSize);
       model.addAttribute("data",page.getList());
       model.addAttribute("page",page);
       model.addAttribute("user",user);
       // 转发
        return "user/data";
    }
    @PostMapping("deletes")
    @ResponseBody
    public Map deletes(Integer[] ids){
        boolean status =userService.deleteByIds(ids);
        Map map = new HashMap();
        map.put("status",status);
        return map;
    }

    @GetMapping(value="navs",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String navs(){
        String str="[{\n" +
                "\t\"title\" : \"系统管理\",\n" +
                "\t\"icon\" : \"icon-computer\",\n" +
                "\t\"href\" : \"page/main.html\",\n" +
                "\t\"spread\" : true,\n" +
                "\t\"children\": [\n" +
                "\t\t{\n" +
                "\t\t\t\"title\" : \"用户管理\",\n" +
                "\t\t\t\"icon\" : \"icon-computer\",\n" +
                "\t\t\t\"href\" : \"system/user/data\"\n" +
                "\t\t},{\n" +
                "\t\t\t\"title\" : \"部门管理\",\n" +
                "\t\t\t\"icon\" : \"icon-computer\",\n" +
                "\t\t\t\"href\" : \"dept/data1.jsp\"\n" +
                "\t\t},\n" +
                "\t\t{\n" +
                "\t\t\t\"title\" : \"角色管理\",\n" +
                "\t\t\t\"icon\" : \"icon-computer\",\n" +
                "\t\t\t\"href\" : \"system/role/data\"\n" +
                "\t\t},\n" +
                "\t\t{\n" +
                "\t\t\t\"title\" : \"资源管理\",\n" +
                "\t\t\t\"icon\" : \"icon-computer\",\n" +
                "\t\t\t\"href\" : \"resource/data.jsp\"\n" +
                "\t\t}\n" +
                "\t]\n" +
                "}]";
        return  str;
    }

    @GetMapping("toEdit")
    public String toEdit(Model model,Integer id){
        // 查询部门信息
        List<SysDept> depts = deptService.selectAllDepts();
        model.addAttribute("depts",depts);
        // 查询角色信息
        List<SysRole> roles = roleService.selectAllRoles();
        model.addAttribute("roles",roles);
        if(id!=null && id!=0){
            SysUser user = userService.selectUserById(id);
            model.addAttribute("user",user);
        }
        return "user/edit";
    }
    @RequestMapping("save")
    @ResponseBody
    public Map save(SysUser user){
        boolean status = userService.saveUser(user);
        Map map = new HashMap();
        map.put("status",status);
        return map;
    }
}
